convert tpg to dynamic Format class. (#1229)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Sat, 18 Nov 2023 22:16:01 +0000 (15:16 -0700)
committerGitHub <noreply@github.com>
Sat, 18 Nov 2023 22:16:01 +0000 (15:16 -0700)
CMakeLists.txt
tpg.cc
tpg.h [new file with mode: 0644]
vecs.cc

index 17b57b335f1ae2693caa529e51c1e20d99ac2759..6be14d1508fe60af2b8d5d1280f9332356104c3d 100644 (file)
@@ -238,6 +238,7 @@ set(HEADERS
   skytraq.h
   subrip.h
   text.h
+  tpg.h
   tpo.h
   unicsv.h
   units.h
diff --git a/tpg.cc b/tpg.cc
index 615a08de2cd8b72ebbe8d2e2b9305b9653a26c66..5b21552e3c19ea2db3beefb363d8007972b055a0 100644 (file)
--- a/tpg.cc
+++ b/tpg.cc
 
  */
 
+#include "tpg.h"
+
 #include <cctype>           // for isalnum
 #include <cstring>          // for memcmp
 
 #include <QChar>            // for QChar
 #include <QString>          // for QString
-#include <QVector>          // for QVector
 
 #include "defs.h"
-#include "gbfile.h"         // for gbfwrite, gbfgetint16, gbfputint16, gbfclose
-#include "jeeps/gpsmath.h"  // for GPS_Lookup_Datum_Index, GPS_Math_Known_Da...
+#include "gbfile.h"         // for gbfwrite, gbfgetint16, gbfputint16, gbfclose, gbfgetdbl, gbfgetpstr, gbfopen_le, gbfputdbl, gbfgetint32, gbfputc, gbfputpstr, gbfread
+#include "jeeps/gpsmath.h"  // for GPS_Lookup_Datum_Index, GPS_Math_Known_Datum_To_WGS84_M, GPS_Math_WGS84_To_Known_Datum_M
 #include "mkshort.h"        // for MakeShort
 
 
 #define MAXTPGSTRINGSIZE       256
 #define MAXTPGOUTPUTPINS       65535
 
-static gbfile* tpg_file_in;
-static gbfile* tpg_file_out;
-static MakeShort* mkshort_handle;
-static char* tpg_datum_opt;
-static int tpg_datum_idx;
-
-static unsigned int waypt_out_count;
-
-static
-QVector<arglist_t> tpg_args = {
-  {"datum", &tpg_datum_opt, "Datum (default=NAD27)", "N. America 1927 mean", ARGTYPE_STRING, ARG_NOMINMAX , nullptr},
-};
-
-static int
-valid_tpg_header(char* header, int len)
+int
+TpgFormat::valid_tpg_header(char* header, int len)
 {
   unsigned char header_bytes[] = { 0xFF, 0xFF, 0x01, 0x00, 0x0D,
                                    0x00, 0x43, 0x54, 0x6F, 0x70,
@@ -68,8 +56,8 @@ valid_tpg_header(char* header, int len)
   return memcmp(header_bytes, header, len);
 }
 
-static void
-tpg_common_init()
+void
+TpgFormat::tpg_common_init()
 {
   tpg_datum_idx = GPS_Lookup_Datum_Index(tpg_datum_opt);
   if (tpg_datum_idx < 0) {
@@ -77,21 +65,21 @@ tpg_common_init()
   }
 }
 
-static void
-tpg_rd_init(const QString& fname)
+void
+TpgFormat::rd_init(const QString& fname)
 {
   tpg_common_init();
   tpg_file_in = gbfopen_le(fname, "rb", MYNAME);
 }
 
-static void
-tpg_rd_deinit()
+void
+TpgFormat::rd_deinit()
 {
   gbfclose(tpg_file_in);
 }
 
-static void
-tpg_wr_init(const QString& fname)
+void
+TpgFormat::wr_init(const QString& fname)
 {
   tpg_common_init();
   tpg_file_out = gbfopen_le(fname, "wb", MYNAME);
@@ -99,15 +87,15 @@ tpg_wr_init(const QString& fname)
   waypt_out_count = 0;
 }
 
-static void
-tpg_wr_deinit()
+void
+TpgFormat::wr_deinit()
 {
   delete mkshort_handle;
   gbfclose(tpg_file_out);
 }
 
-static void
-tpg_read()
+void
+TpgFormat::read()
 {
   char buff[MAXTPGSTRINGSIZE + 1];
   double amt;
@@ -169,8 +157,8 @@ tpg_read()
   }
 }
 
-static void
-tpg_waypt_pr(const Waypoint* wpt)
+void
+TpgFormat::tpg_waypt_pr(const Waypoint* wpt)
 {
   double lon, lat;
   double amt;
@@ -280,8 +268,8 @@ tpg_waypt_pr(const Waypoint* wpt)
   }
 }
 
-static void
-tpg_write()
+void
+TpgFormat::write()
 {
   unsigned char header_bytes[] = { 0xFF, 0xFF, 0x01, 0x00, 0x0D,
                                    0x00, 0x43, 0x54, 0x6F, 0x70,
@@ -307,19 +295,8 @@ tpg_write()
   /* write the rest of the header */
   gbfwrite(header_bytes, 1, 19, tpg_file_out);
 
-  waypt_disp_all(tpg_waypt_pr);
+  auto tpg_waypt_pr_lambda = [this](const Waypoint* waypointp)->void {
+    tpg_waypt_pr(waypointp);
+  };
+  waypt_disp_all(tpg_waypt_pr_lambda);
 }
-
-ff_vecs_t tpg_vecs = {
-  ff_type_file,
-  FF_CAP_RW_WPT,
-  tpg_rd_init,
-  tpg_wr_init,
-  tpg_rd_deinit,
-  tpg_wr_deinit,
-  tpg_read,
-  tpg_write,
-  nullptr,
-  &tpg_args,
-  NULL_POS_OPS
-};
diff --git a/tpg.h b/tpg.h
new file mode 100644 (file)
index 0000000..6f4b098
--- /dev/null
+++ b/tpg.h
@@ -0,0 +1,84 @@
+/*
+    National Geographic Topo! TPG file support (Waypoints/Routes)
+    Contributed to gpsbabel by Alex Mottram
+
+    For Topo! version 2.x.  Routes are currently not implemented.
+
+    Copyright (C) 2002 Alex Mottram, geo_alexm at cox-internet.com
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+ */
+#ifndef TPG_H_INCLUDED_
+#define TPG_H_INCLUDED_
+
+#include <QString>    // for QString
+#include <QVector>    // for QVector
+
+#include "defs.h"
+#include "format.h"   // for Format
+#include "gbfile.h"   // for gbfile
+#include "mkshort.h"  // for MakeShort
+
+
+class TpgFormat : public Format
+{
+public:
+  using Format::Format;
+
+  QVector<arglist_t>* get_args() override
+  {
+    return &tpg_args;
+  }
+
+  ff_type get_type() const override
+  {
+    return ff_type_file;
+  }
+
+  QVector<ff_cap> get_cap() const override
+  {
+    return FF_CAP_RW_WPT;
+  }
+
+  void rd_init(const QString& fname) override;
+  void read() override;
+  void rd_deinit() override;
+  void wr_init(const QString& fname) override;
+  void write() override;
+  void wr_deinit() override;
+
+private:
+  /* Member Functions */
+
+  static int valid_tpg_header(char* header, int len);
+  void tpg_common_init();
+  void tpg_waypt_pr(const Waypoint* wpt);
+
+  /* Data Members */
+
+  gbfile* tpg_file_in{};
+  gbfile* tpg_file_out{};
+  MakeShort* mkshort_handle{};
+  char* tpg_datum_opt{};
+  int tpg_datum_idx{};
+
+  unsigned int waypt_out_count{};
+
+  QVector<arglist_t> tpg_args = {
+    {"datum", &tpg_datum_opt, "Datum (default=NAD27)", "N. America 1927 mean", ARGTYPE_STRING, ARG_NOMINMAX, nullptr},
+  };
+};
+#endif // TPG_H_INCLUDED_
diff --git a/vecs.cc b/vecs.cc
index 06bee8ecc69702f96e3eee86ef89f678c247cea8..4807ad9496723e4a72477e9e11a1e30d5a5809cd 100644 (file)
--- a/vecs.cc
+++ b/vecs.cc
@@ -70,6 +70,7 @@
 #include "src/core/logging.h"  // for Warning, FatalMsg
 #include "subrip.h"            // for SubripFormat
 #include "text.h"              // for TextFormat
+#include "tpg.h"               // for TpgFormat
 #include "tpo.h"               // for Tpo2Format, Tpo3Format
 #include "unicsv.h"            // for UnicsvFormat
 #include "v900.h"              // for V900Format
@@ -81,7 +82,6 @@
 extern ff_vecs_t geo_vecs;
 extern ff_vecs_t ozi_vecs;
 #if MAXIMAL_ENABLED
-extern ff_vecs_t tpg_vecs;
 extern ff_vecs_t gpl_vecs;
 extern ff_vecs_t mtk_vecs;
 extern ff_vecs_t mtk_fvecs;
@@ -121,7 +121,6 @@ struct Vecs::Impl {
   KmlFormat kml_fmt;
 #if MAXIMAL_ENABLED
   LowranceusrFormat lowranceusr_fmt;
-  LegacyFormat tpg_fmt {tpg_vecs};
   Tpo2Format tpo2_fmt;
   Tpo3Format tpo3_fmt;
 #if SHAPELIB_ENABLED
@@ -233,11 +232,12 @@ struct Vecs::Impl {
       nullptr,
     },
     {
-      &tpg_fmt,
+      nullptr,
       "tpg",
       "National Geographic Topo .tpg (waypoints)",
       "tpg",
       nullptr,
+      &fmtfactory<TpgFormat>
     },
     {
       &tpo2_fmt,